package com.weihuachao.repair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.weihuachao.app.application;

import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lidroid.xutils.http.RequestParams;
import com.weihuachao.entity.Result;
import com.weihuachao.entity.User;
import com.weihuachao.https.BasicRequestCallBck;
import com.weihuachao.https.XUtils;

/**
 * Created by Virgo on 2016/7/30.
 */
public class LoginActivity extends Activity {
    private Button login;
    private Button cancel;
    private EditText username;
    private EditText password;
    private TextView lo_re;

    protected void onCreate(Bundle savedInstanceState) {
        XUtils.init(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        login = (Button) findViewById(R.id.login);
        cancel = (Button) findViewById(R.id.Cancel_login);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        lo_re = (TextView) findViewById(R.id.login_register);
        lo_re.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                return false;
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginUser();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    public void LoginUser() {
        final String user = username.getText().toString();
        String pass = password.getText().toString();
        RequestParams params = new RequestParams();
        params.addBodyParameter("user.phone", user);
        params.addBodyParameter("user.pwd", pass);
        XUtils.send(XUtils.LOGIN, params, new BasicRequestCallBck<Result<User>>() {
            public void success(Result<User> data) {
                if (data.state == 1) {
                    application.setAccount(data.data.getPhone());
                    application.setUid(data.data.getUid());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        });
    }
}
